package org.example.likou150.circularList;

// https://leetcode.cn/problems/merge-two-sorted-lists/?envType=study-plan-v2&envId=top-interview-150
public class MergeTwoLists {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode head = new ListNode();
        ListNode current = head;
        Integer e1 = null;
        Integer e2 = null;
        while (list1!=null || list2!=null){
            if(list1==null){
                e2 = list2.val;
                current.next=new ListNode(e2);
                current = current.next;
                list2 = list2.next;
            }else if(list2==null) {
                e1 = list1.val;
                current.next=new  ListNode(e1);
                current = current.next;
                list1 = list1.next;
            }else {
                e1 = list1.val;
                e2 = list2.val;
                if(e1 > e2){
                    current.next=new ListNode(e2);
                    current = current.next;
                    list2 = list2.next;
                }else {
                    current.next=new ListNode(e1);
                    current = current.next;
                    list1 = list1.next;
                }
            }
        }
        return head.next;
    }

private class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
}
